package inet.app.model.util;

import inet.app.R;
import inet.app.common.DeveloperConfig;
import inet.app.model.cau.CauPosition;
import inet.app.model.cau.Global;
import inet.app.model.object.SMSData;
import inet.app.view.activity.FlashScreen;
import inet.app.view.activity.MyApp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.Normalizer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.velocity.runtime.log.Log;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Util {
	public final static int MEGA_BYTE = 1024 * 1024;
	public int Red1 = Color.rgb(255, 0, 0);
	ProgressDialog dialog;
	final static String PHONE = "8785";
	private final static String[] VINA = { "091", "094", "0123", "0125",
			"0127", "0129", "" };
	private final static String[] VIETTEL = { "097", "098", "0169", "0168",
			"0167", "0166", "0165", "0164", "0163" };
	private final static String[] MOBI = { "090", "093", "0122", "0126",
			"0128", "0121", "0120" };
	private final static String[] VIETNAMMOBILE = { "092", "0188" };
	private final static String[] BEELINE = { "0199" };
	private final static String[] SFONE = { "095" };
	private final static String[] EVN = { "096" };
	private static final int MODE_WORLD_READABLE = 0;

	// slidemenu
	public static final String MENU_0 = "Kết Quả";
	public static final String MENU_1 = "VIP";
	public static final String MENU_2 = "Sổ Mơ";
	public static final String MENU_3 = "Thống Kê";
	public static final String MENU_4 = "Lịch Mở Thưởng";
	public static final String MENU_5 = "Dò Số";
	public static final String MENU_6 = "Chọn Vé Số";
	public static final String MENU_7 = "Quay Thử";
	public static final String MENU_8 = "Chia sẻ";
	public static final String MENU_9 = "Hướng Dẫn";
	public static final String MENU_10 = "Đăng Nhập, Đăng Ký";
	public static final String MENU_10X = "Hi, ";
	public static final String MENU_11 = "Thoát!";
	
	public static final String MENU_MOI_BAN_BE = "Mời bạn bè";

	private static char[] SPECIAL_CHARACTERS = { '!', '"', '#', '$', '%', '*',
			'+', ',', ':', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '`',
			'|', '~', 'À', 'Á', 'Â', 'Ã', 'È', 'É', 'Ê', 'Ì', 'Í', 'Ò', 'Ó',
			'Ô', 'Õ', 'Ù', 'Ú', 'Ý', 'à', 'á', 'â', 'ã', 'è', 'é', 'ê', 'ì',
			'í', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'ý', 'Ă', 'ă', 'Đ', 'đ', 'Ĩ',
			'ĩ', 'Ũ', 'ũ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ạ', 'ạ', 'Ả', 'ả', 'Ấ', 'ấ',
			'Ầ', 'ầ', 'Ẩ', 'ẩ', 'Ẫ', 'ẫ', 'Ậ', 'ậ', 'Ắ', 'ắ', 'Ằ', 'ằ', 'Ẳ',
			'ẳ', 'Ẵ', 'ẵ', 'Ặ', 'ặ', 'Ẹ', 'ẹ', 'Ẻ', 'ẻ', 'Ẽ', 'ẽ', 'Ế', 'ế',
			'Ề', 'ề', 'Ể', 'ể', 'Ễ', 'ễ', 'Ệ', 'ệ', 'Ỉ', 'ỉ', 'Ị', 'ị', 'Ọ',
			'ọ', 'Ỏ', 'ỏ', 'Ố', 'ố', 'Ồ', 'ồ', 'Ổ', 'ổ', 'Ỗ', 'ỗ', 'Ộ', 'ộ',
			'Ớ', 'ớ', 'Ờ', 'ờ', 'Ở', 'ở', 'Ỡ', 'ỡ', 'Ợ', 'ợ', 'Ụ', 'ụ', 'Ủ',
			'ủ', 'Ứ', 'ứ', 'Ừ', 'ừ', 'Ử', 'ử', 'Ữ', 'ữ', 'Ự', 'ự', };

	private static char[] REPLACEMENTS = { '\0', '\0', '\0', '\0', '\0', '\0',
			'_', '\0', '_', '\0', '\0', '\0', '\0', '\0', '\0', '_', '\0',
			'\0', '\0', '\0', '\0', 'A', 'A', 'A', 'A', 'E', 'E', 'E', 'I',
			'I', 'O', 'O', 'O', 'O', 'U', 'U', 'Y', 'a', 'a', 'a', 'a', 'e',
			'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'y', 'A', 'a',
			'D', 'd', 'I', 'i', 'U', 'u', 'O', 'o', 'U', 'u', 'A', 'a', 'A',
			'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a',
			'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'E', 'e', 'E', 'e', 'E',
			'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'I', 'i',
			'I', 'i', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O',
			'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o',
			'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U',
			'u', };

	private static SharedPreferences sharedPreferences;

	public static String BongCouple(int number) {
		String result = null;
		if (number < 10)
			result = "0" + number;
		else
			result = "" + number;

		if (result.substring(0, 1).equals(result.substring(1, 2))) {
			return result;
		} else {
			return result + ", " + result.substring(1, 2)
					+ result.substring(0, 1);
		}
	}

	public static String Inverse(String couple) {
		if (couple == null | couple.length() < 2)
			return null;
		return couple.substring(1, 2) + "" + couple.substring(0, 1);
	}

	public static void updateTime(TextView tv, String alert) {
		Date date = new Date();
		int hour = date.getHours();
		int min = date.getMinutes();
		if (min > 9)
			tv.setText(alert + " " + hour + ":" + min);
		else
			tv.setText(alert + " " + hour + ":0" + min);
	}

	public static void sendsms(String message, final Activity ac) {
		if (!DeveloperConfig.ENABLE_SMS) {
			return;
		}
		Uri uri = Uri.parse("smsto:" + PHONE);
		Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
		intent.putExtra("sms_body", message);
		ac.startActivity(intent);

	}

	public static void sendSMS(String message, String phone, final Activity ctx) {
		// ---sends an SMS message to another device---
		if (!DeveloperConfig.ENABLE_SMS) {
			return;
		}
		if (isUnableSms(ctx)) {
			Toast.makeText(
					ctx,
					ctx.getString(R.string.thiet_bi_cua_ban_khong_ho_tro_gui_tin_nhan),
					Toast.LENGTH_LONG).show();
			return;
		}
		String PHONE = phone;
		String SENT = "SMS_SENT";
		PendingIntent sentPI = PendingIntent.getBroadcast(ctx, 0, new Intent(
				SENT), 0);
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(PHONE, null, message, sentPI, null);
		System.out.println("GỬI THÀNH CÔNG TIN NHẮN " + message + " ĐẾN "
				+ phone);
	}

	public static void showDialog(int i, final Activity act) {
		Dialog dialog = new Dialog(act);
		dialog.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(50, 255, 255, 255)));
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		if (i == 0)
			builder.setMessage("Lỗi kết nối!");
		if (i == 1)
			builder.setMessage("Dữ liệu đang cập nhật!");
		if (i == 2)
			builder.setMessage("Không có kết quả ngày này!");
		if (i == 3)
			builder.setMessage("Login False!");
		if (i == 4)
			builder.setMessage("Đăng ký thất bại!");
		if (i == 5)
			builder.setMessage("Kích hoạt thất bại!");
		if (i == 6)
			builder.setMessage("Bạn chưa nhập cặp số!");
		if (i == 7)
			builder.setMessage("Nhập cặp Lô chưa đúng!");
		if (i == 8)
			builder.setMessage("Không tìm thấy nội dung!");
		if (i == 9)
			builder.setMessage("Đăng nhập để xem thông tin !");
		builder.setPositiveButton("Đóng cửa sổ", new Dialog.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
	public static void showDialog(String msg, final Activity act) {
		Dialog dlog = new Dialog(act);
		dlog.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(50, 255, 255, 255)));
		dlog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		builder.setMessage(msg);
		builder.setPositiveButton("Đóng cửa sổ", new Dialog.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
//		builder.show();
		 AlertDialog alert = builder.create();
		 alert.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
	        alert.setIcon(R.drawable.common_notification);
	        alert.setTitle("Thông báo");
	        alert.show();
	}
	/**
	 * Function to convert milliseconds time to Timer Format
	 * Hours:Minutes:Seconds
	 * */
	public static String milliSecondsToTimer(long milliseconds) {
		String finalTimerString = "";
		String secondsString = "";

		// Convert total duration into time
		int hours = (int) (milliseconds / (1000 * 60 * 60));
		int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
		int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
		// Add hours if there
		if (hours > 0) {
			finalTimerString = hours + ":";
		}
		// Prepending 0 to seconds if it is one digit
		if (seconds < 10) {
			secondsString = "0" + seconds;
		} else {
			secondsString = "" + seconds;
		}

		finalTimerString = finalTimerString + minutes + ":" + secondsString;

		// return timer string
		return finalTimerString;
	}

	/**
	 * Function to get Progress percentage
	 * 
	 * @param currentDuration
	 * @param totalDuration
	 * */
	public static int getProgressPercentage(long currentDuration,
			long totalDuration) {
		Double percentage = (double) 0;
		long currentSeconds = (int) (currentDuration / 1000);
		long totalSeconds = (int) (totalDuration / 1000);
		// calculating percentage
		percentage = (((double) currentSeconds) / totalSeconds) * 100;

		// return percentage
		return percentage.intValue();
	}

	/**
	 * Function to change progress to timer
	 * 
	 * @param progress
	 *            -
	 * @param totalDuration
	 *            returns current duration in milliseconds
	 * */
	public static int progressToTimer(int progress, int totalDuration) {
		int currentDuration = 0;
		totalDuration = (int) (totalDuration / 1000);
		currentDuration = (int) ((((double) progress) / 100) * totalDuration);
		// return current duration in milliseconds
		return currentDuration * 1000;
	}

	public static char removeAccent(char ch) {
		int index = Arrays.binarySearch(SPECIAL_CHARACTERS, ch);
		if (index >= 0) {
			ch = REPLACEMENTS[index];
		}
		return ch;
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public static String removeAccents(String accented) {
		// convert accented chars to equivalent unaccented char + dead char
		// accent pair.
		// See http://www.unicode.org/unicode/reports/tr15/tr15-23.html no
		// understand the NFD transform.
		final String normalized = Normalizer.normalize(accented,
				Normalizer.Form.NFD);
		// remove the dead char accents, leaving just the unaccented chars.
		// Stripped string should have the same length as the original accented
		// String.
		StringBuilder sb = new StringBuilder(accented.length());
		for (int i = 0; i < normalized.length(); i++) {
			char c = normalized.charAt(i);
			if (Character.getType(c) != Character.NON_SPACING_MARK) {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static String convertUTF8(String s) {
		StringBuilder sb = new StringBuilder(s);
		for (int i = 0; i < sb.length(); i++) {
			sb.setCharAt(i, removeAccent(sb.charAt(i)));
		}
		return sb.toString();
	}

	public static String getDevice() {

		StringBuffer buf = new StringBuffer();
		buf.append("\nVERSION.INCREMENTAL {" + Build.VERSION.INCREMENTAL + "}");
		buf.append("\nVERSION.SDK {" + Build.VERSION.SDK + "}");
		buf.append("\nBOARD {" + Build.BOARD + "}");
		buf.append("\nBRAND {" + Build.BRAND + "}");
		buf.append("\nFINGERPRINT {" + Build.FINGERPRINT + "}");
		buf.append("\nHOST {" + Build.HOST + "}");
		buf.append("\nID {" + Build.ID + "}");
		buf.append("\nUSER {" + Build.USER + "}");
		return buf.toString();
	}

	public static String editResponse(String response) {
		char[] ch = response.toCharArray();
		String str = "";
		for (int i = 0; i < ch.length; i++) {
			if (i % 2 != 0)
				str = str + ch[i];
		}
		return str;
	}

	public static void ClearNotifi(NotificationManager mNotificationManager,
			int NOTIF_ID) {
		mNotificationManager.cancel(NOTIF_ID);
	}

	public static void refreshApp(Activity ac) {
		Intent i = new Intent();
		i.setAction(Intent.ACTION_MAIN);
		i.addCategory(Intent.CATEGORY_HOME);
		ac.startActivity(i);
		ac.finish();
	}

	public static void exitApp(Activity ac) {
		Intent i = new Intent();
		i.setAction(Intent.ACTION_MAIN);
		i.addCategory(Intent.CATEGORY_HOME);
		ac.startActivity(i);
		ac.finish();
	}

	public static Drawable getImage(String link) {
		URLConnection url;
		Drawable drawAble = null;
		try {
			String srcName = link;
			url = new URL(link).openConnection();
			url.setConnectTimeout(1000);
			url.setReadTimeout(500);
			InputStream is = url.getInputStream();
			drawAble = Drawable.createFromStream(is, srcName);
			return drawAble;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static String readFilePrivate(Activity ac) {
		Context ctx = ac.getApplicationContext();
		String key = "";
		try {
			StringBuilder inb = new StringBuilder();
			InputStreamReader inputReader = new InputStreamReader(
					ctx.openFileInput("login.txt"));
			int ch;
			while ((ch = inputReader.read()) != -1)
				inb.append((char) ch);
			key = inb.toString().trim();
			inputReader.close();
			System.out.println("exits===" + key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			key = "0- noname";
			System.out.println("no exits ===" + key);
		}
		System.out.println("freee===" + key);
		return key;
	}

	public static void writeFilePrivate(String count, Activity ac) {
		Context ctx = ac.getApplicationContext();
		try {
			FileOutputStream fos = ctx.openFileOutput("login.txt",
					Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
			fos.write(count.getBytes());
			// fos.write(content);
			fos.close();
		} catch (java.io.FileNotFoundException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
	}

	public static void writeFilePrivate(String name, String content, Activity ac) {
		Context ctx = ac.getApplicationContext();
		String str = content;
		if (readFilePrivate(name, ac) != null)
			str = "," + content;

		try {
			FileOutputStream fos = ctx
					.openFileOutput(name, Context.MODE_APPEND);
			fos.write(str.getBytes());
			// fos.write(content);
			fos.close();
		} catch (java.io.FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static File myFile = new File("/sdcard/Log2file.txt");

	public static void Log2File(String msg) {
		// Context ctx = ac.getApplicationContext();
		try {
			if (!myFile.delete()) {
				myFile.createNewFile();
			}
			FileOutputStream fOut = new FileOutputStream(myFile);
			OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
			myOutWriter.append(msg);
			myOutWriter.close();
			fOut.close();
		} catch (Exception e) {
		}
	}

	public static String readFilePrivate(String name, Activity ac) {
		Context ctx = ac.getApplicationContext();
		String key = null;
		try {
			StringBuilder inb = new StringBuilder();
			InputStreamReader inputReader = new InputStreamReader(
					ctx.openFileInput(name));
			int ch;
			while ((ch = inputReader.read()) != -1)
				inb.append((char) ch);
			key = inb.toString().trim();
			inputReader.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return key;
	}

	public static String readFilePrivateTab(Activity ac) {
		Context ctx = ac.getApplicationContext();
		String key = "";
		try {
			StringBuilder inb = new StringBuilder();
			InputStreamReader inputReader = new InputStreamReader(
					ctx.openFileInput("tab.txt"));
			int ch;
			while ((ch = inputReader.read()) != -1)
				inb.append((char) ch);
			key = inb.toString().trim();
			inputReader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			key = "0";
		}
		return key;
	}

	public static String readFilePrivateTab(String fileName, Activity ac) {
		Context ctx = ac.getApplicationContext();
		String key = "";
		try {
			StringBuilder inb = new StringBuilder();
			InputStreamReader inputReader = new InputStreamReader(
					ctx.openFileInput(fileName + ".txt"));
			int ch;
			while ((ch = inputReader.read()) != -1)
				inb.append((char) ch);
			key = inb.toString().trim();
			inputReader.close();
			System.out.println("exits===" + key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			key = "0";
			System.out.println("no exits ===" + key);
		}
		System.out.println("freee===" + key);
		return key;
	}

	public static void writeFilePrivateTab(String count, Activity ac) {
		Context ctx = ac.getApplicationContext();
		try {
			FileOutputStream fos = ctx.openFileOutput("tab.txt",
					Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
			fos.write(count.getBytes());
			// fos.write(content);
			fos.close();
		} catch (java.io.FileNotFoundException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
	}

	public static void writeFilePrivateTab(String fileName, String count,
			Activity ac) {
		Context ctx = ac.getApplicationContext();
		try {
			FileOutputStream fos = ctx.openFileOutput(fileName + ".txt",
					Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
			fos.write(count.getBytes());
			fos.close();
		} catch (java.io.FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	public static String getDay(String day) {
		String d = "";
		// System.out.println("day"+day);
		SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date d1 = null;
		Calendar tdy1;

		try {
			d1 = form.parse(day);
			// System.out.println(d1.toString());
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tdy1 = Calendar.getInstance();
		tdy1.setTime(d1);
		int a = tdy1.get(Calendar.DAY_OF_WEEK);
		switch (a) {
		case 1:
			d = "Chủ nhật";
			break;
		case 2:
			d = "Thứ hai";
			break;
		case 3:
			d = "Thứ ba";
			break;
		case 4:
			d = "Thứ tư";
			break;
		case 5:
			d = "Thứ năm";
			break;
		case 6:
			d = "Thứ sáu";
			break;
		case 7:
			d = "Thứ bảy";
			break;

		default:
			break;
		}
		return d;
	}

	public static boolean ketnoi(Context context) {
		// TODO Auto-generated method stub
		ConnectivityManager connec = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		// ARE WE CONNECTED TO THE NET
		boolean isConnect3G = false;
		boolean isConnectWifi = false;

		// check connect 3g
		if (connec.getNetworkInfo(0) != null) {
			if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
					|| connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING) {
				isConnect3G = true;
			}
		}

		// check connect wifi
		if (connec.getNetworkInfo(1) != null) {
			if (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED
					|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
				isConnectWifi = true;
			}
		}

		return (isConnect3G || isConnectWifi);
	}

	public static void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static boolean isSMSReceiveTimeout(long time) {
		return (time > 60000);
	}

	public static boolean onClick_MB() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1800 && hhmm < 1845)
			return true;
		return false;
	}

	public static boolean onClick_MT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1700 && hhmm < 1745)
			return true;
		return false;
	}

	public static boolean onClick_MN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1600 && hhmm < 1645)
			return true;
		return false;
	}

	public static boolean onTT_MB() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm == 1800)
			return true;
		return false;
	}

	public static boolean onTT_MT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm == 1700)
			return true;
		return false;
	}

	public static boolean onTT_MN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm == 1600)
			return true;
		return false;
	}

	private static StringBuilder log;
	static File file = new File("/sdcard/Log3file.txt");

	// create text file in SDCard
	public static void Logfile(String name) {
		try {
			Process process = Runtime.getRuntime().exec("logcat -d");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(process.getInputStream()));

			log = new StringBuilder();
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				log.append(line);
			}
		} catch (IOException e) {
		}
		// convert log to string
		final String logString = new String(log.toString());

		// //create text file in SDCard
		// File sdCard = Environment.getExternalStorageDirectory();
		// File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
		// dir.mkdirs();
		// File file = new File(dir, name+".txt");

		if (!file.delete()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			// to write logcat in text file
			FileOutputStream fOut = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			// Write the string to the file
			osw.write(logString);
			osw.flush();
			osw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getMobile(String mobile) {
		String mang = "";

		for (int i = 0; i < VINA.length; i++) {
			if (mobile.indexOf(VINA[i]) == 0) {
				mang = "vina";
				break;
			}
		}
		for (int i = 0; i < VIETTEL.length; i++) {
			if (mobile.indexOf(VIETTEL[i]) == 0) {
				mang = "viettel";
				break;
			}
		}
		for (int i = 0; i < MOBI.length; i++) {
			if (mobile.indexOf(MOBI[i]) == 0) {
				mang = "mobi";
				break;
			}
		}
		for (int i = 0; i < VIETNAMMOBILE.length; i++) {
			if (mobile.indexOf(VIETNAMMOBILE[i]) == 0) {
				mang = "vmm";
				break;
			}
		}

		// for(int i=0;i<BEELINE.length;i++) {
		// if(mobile.indexOf(BEELINE[i])==0)
		// {
		// mang="BEELINE";
		// break;
		// }
		// }
		// for(int i=0;i<EVN.length;i++) {
		// if(mobile.indexOf(EVN[i])==0)
		// {
		// mang="EVN";
		// break;
		// }
		// }
		// for(int i=0;i<SFONE.length;i++) {
		// if(mobile.indexOf(SFONE[i])==0)
		// {
		// mang="SFONE";
		// break;
		// }
		// }
		return mang;
	}

	@SuppressLint("WorldReadableFiles")
	public static void SharedPreferences(String nhamang, Activity c) {
		SharedPreferences userDetails = c.getSharedPreferences("userdetails",
				MODE_WORLD_READABLE);
		Editor edit = userDetails.edit();
		edit.clear();
		edit.putString("nhamang", nhamang);
		edit.commit();
	}

	public static String getSharePreferences(Activity c) {
		// Gét nhà mạng
		String nhamang = "";
		SharedPreferences userDetails = c.getSharedPreferences("userdetails",
				MODE_WORLD_READABLE);
		return nhamang = userDetails.getString("nhamang", "");
	}

	public static void SharedPreferencesSub(String data, String filename,
			Activity c) {
		SharedPreferences userDetails = c.getSharedPreferences(filename,
				MODE_WORLD_READABLE);
		Editor edit = userDetails.edit();
		edit.clear();
		edit.putString("dayofmonth", data);
		edit.commit();
	}

	public static String getSharePreferencesSub(Activity c, String filename) {

		// Gét nhà mạng
		SharedPreferences userDetails = c.getSharedPreferences(filename,
				MODE_WORLD_READABLE);
		return userDetails.getString("dayofmonth", "");
	}

	public static void savePreferences(String key, boolean value,
			Context context) {

		if (sharedPreferences == null) {
			sharedPreferences = PreferenceManager
					.getDefaultSharedPreferences(context);
		}
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

//	public static void savePosAudio(String key, int value, Context context) {
//		if (sharedPreferences == null) {
//			sharedPreferences = PreferenceManager
//					.getDefaultSharedPreferences(context);
//		}
//		Editor editor = sharedPreferences.edit();
//		editor.putInt(key, value);
//		editor.commit();
//	}
//
//	public static int getPosAudio(String key, Context context) {
//
//		if (sharedPreferences == null) {
//			sharedPreferences = PreferenceManager
//					.getDefaultSharedPreferences(context);
//		}
//
//		int value = sharedPreferences.getInt(key, 0);
//		return value;
//	}

	public static boolean getPreferences(String key, Context context) {

		if (sharedPreferences == null) {
			sharedPreferences = PreferenceManager
					.getDefaultSharedPreferences(context);
		}
		boolean value = sharedPreferences.getBoolean(key, false);
		return value;
	}

	public static boolean getdataSms(Activity ctx, String data) {
		// List<SMSData> smsList = new ArrayList<SMSData>();
		Uri uri = Uri.parse("content://sms/inbox");
		Cursor c = ctx.getContentResolver().query(uri, null, null, null, null);
		ctx.startManagingCursor(c);
		// Read the sms data and store it in the list
		if (c.moveToFirst()) {
			for (int i = 0; i < c.getCount(); i++) {
				SMSData sms = new SMSData();
				sms.setBody(c.getString(c.getColumnIndexOrThrow("body"))
						.toString());
				sms.setNumber(c.getString(c.getColumnIndexOrThrow("address"))
						.toString());
				if (c.getString(c.getColumnIndexOrThrow("address")).toString()
						.contains("8985")
						&& c.getString(c.getColumnIndexOrThrow("body"))
								.toString()
								.toLowerCase()
								.contains(
										("Quy khach da huy thanh cong" + data)
												.toLowerCase())) {
					System.out
							.println("+++++++++++++++++++++++++++++++++++++++++Chuẩn rồi");
					System.out.println("NUMBER "
							+ i
							+ " :"
							+ c.getString(c.getColumnIndexOrThrow("address"))
									.toString());
					System.out.println("CONTENT "
							+ i
							+ " :"
							+ c.getString(c.getColumnIndexOrThrow("body"))
									.toString());
					return true;
				} else {
					System.out.println("==> Khong rồi");
					System.out.println("==> NUMBER "
							+ i
							+ " :"
							+ c.getString(c.getColumnIndexOrThrow("address"))
									.toString());
					System.out.println("==> CONTENT "
							+ i
							+ " :"
							+ c.getString(c.getColumnIndexOrThrow("body"))
									.toString());
					c.moveToNext();
				}
			}
		}
		c.close();
		return false;
	}

	public static String Macodebydate_MN(String date) {
		String str_result = "";
		Calendar caln = Calendar.getInstance();
		SimpleDateFormat dfn = new SimpleDateFormat("dd/MM/yyyy");
		// String ngayofdatepicker = dfn.format(caln.getTime());
		String temp[];
		temp = date.split("/");
		final Calendar cal_thu = Calendar.getInstance();
		cal_thu.set(Integer.parseInt(temp[2]), Integer.parseInt(temp[1]) - 1,
				Integer.parseInt(temp[0].substring(temp[0].length() - 2,
						temp[0].length())));
		int thuofngay = cal_thu.get(Calendar.DAY_OF_WEEK);
		System.out.println("THỨ CỦA NGÀY LÀ " + thuofngay);
		if (thuofngay == 1) {
			str_result = "XSTG" + ", " + "XSKG" + ", " + "XSDL";
		} else if (thuofngay == 7) {
			str_result = "XSHCM" + ", " + "XSLA" + ", " + "XSBP" + ", "
					+ "XSHG";
		} else if (thuofngay == 6) {
			str_result = "XSVL" + ", " + "XSBD" + ", " + "XSTV";
		} else if (thuofngay == 5) {
			str_result = "XSAG" + ", " + "XSTN" + ", " + "XSBTH";
		} else if (thuofngay == 4) {
			str_result = "XSDN" + ", " + "XSST" + ", " + "XSCT";
		} else if (thuofngay == 3) {
			str_result = "XSBTR" + ", " + "XSVT" + ", " + "XSBL";
		} else if (thuofngay == 2) {
			str_result = "XSHCM" + ", " + "XSDT" + ", " + "XSCM";
		}
		return str_result;
	}

	public static String Macodebydate_MT(String date) {
		String str_result = "";
		Calendar caln = Calendar.getInstance();
		SimpleDateFormat dfn = new SimpleDateFormat("dd/MM/yyyy");
		// String ngayofdatepicker = dfn.format(caln.getTime());
		String temp[];
		temp = date.split("/");
		final Calendar cal_thu = Calendar.getInstance();
		cal_thu.set(Integer.parseInt(temp[2]), Integer.parseInt(temp[1]) - 1,
				Integer.parseInt(temp[0].substring(temp[0].length() - 2,
						temp[0].length())));
		int thuofngay = cal_thu.get(Calendar.DAY_OF_WEEK);
		System.out.println("THỨ CỦA NGÀY LÀ " + thuofngay);
		if (thuofngay == 1) {
			str_result = "XSKH" + ", " + "XSKT";
		} else if (thuofngay == 7) {
			str_result = "XSDNG" + ", " + "XSQNI" + ", " + "XSDNO";
		} else if (thuofngay == 6) {
			str_result = "XSGL" + ", " + "XSNT";
		} else if (thuofngay == 5) {
			str_result = "XSBDH" + ", " + "XSQB" + ", " + "XSQT";
		} else if (thuofngay == 4) {
			str_result = "XSKH" + ", " + "XSDNG";
		} else if (thuofngay == 3) {
			str_result = "XSDLK" + ", " + "XSQNM";
		} else if (thuofngay == 2) {
			str_result = "XSPY" + ", " + "XSTTH";
		}
		return str_result;
	}

	public static boolean onLiveTTTT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1610 && hhmm < 1835)
			return true;
		return false;
	}

	public static boolean onLiveTime() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1810 && hhmm < 1835)
			return true;
		return false;
	}
	public static boolean onNullMB() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm >= 1810 && hhmm <= 1815)
			return true;
		return false;
	}
	public static boolean onNullMT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm >= 1710 && hhmm <= 1715)
			return true;
		return false;
	}
	public static boolean onNullMN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm >= 1610 && hhmm <= 1615)
			return true;
		return false;
	}
	public static boolean onLiveTimeMN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1610 && hhmm < 1635)
			return true;
		return false;
	}

	public static boolean onLiveTimeMT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return true;
		}
		if (hhmm > 1710 && hhmm < 1735)
			return true;
		return false;
	}

	public static boolean onBeforeLiveTime() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm <= 1810)
			return true;
		return false;
	}

	public static boolean onBeforeLiveTimeMN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm <= 1610)
			return true;
		return false;
	}

	public static boolean onBeforeLiveTimeMT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm <= 1710)
			return true;
		return false;
	}

	public static boolean onAfterLiveTime() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm > 1810)
			return true;
		return false;
	}

	public static boolean onAfterLiveTimeMT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));

		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm > 1710)
			return true;
		return false;
	}

	public static boolean onAfterLiveTimeMN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm > 1610)
			return true;
		return false;
	}

	public static boolean onLiveTime_MB() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (hhmm > 1810 && hhmm < 1835)
			return true;
		return false;
	}

	public static boolean onLiveTime_MT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (hhmm > 1710 && hhmm < 1735)
			return true;
		return false;
	}

	public static boolean onLiveTime_MN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));
		if (hhmm > 1610 && hhmm < 1645)
			return true;
		return false;
	}

	public static boolean finishLiveMB() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));

		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm > 1835)
			return true;
		return false;
	}

	public static boolean finishLiveMT() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));

		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm > 1735)
			return true;
		return false;
	}

	public static boolean finishLiveMN() {
		int hhmm = Integer.parseInt(DateProc
				.Timestamp2HHMM(DateProc.createTimestamp()).replaceAll(":", "")
				.replaceAll(" ", ""));

		if (MyApp.isDebugTuongThuatTT) {
			return false;
		}
		if (hhmm >= 1640)
			return true;
		return false;
	}

	public static int SumCouple(String str) {

		if (str == null || str.equals("") || str.length() < 2)
			return 0;
		int sum = Integer.parseInt(str.substring(0, 1))
				+ Integer.parseInt(str.substring(1, 2));

		if (sum > 9) {
			sum = Integer.parseInt(("" + sum).substring(1, 2));
		}
		return sum;

	}

	public static <T> void getInfoMemory(Class<T> reference) {
		Runtime rt = Runtime.getRuntime();
		float totalMemory = rt.totalMemory() * 1.0f / MEGA_BYTE;
		float freeMemory = rt.freeMemory() * 1.0f / MEGA_BYTE;
	}

	public static void Notification(Context context, boolean quit, String json) {
		Intent notifyIntent = null;
		Bundle b = new Bundle();
		CharSequence contentTitle = "Tin Xổ Số";
		Notification notifyDetails = new Notification(R.drawable.icon_kqxs,
				"Tin Xổ Số", System.currentTimeMillis());
		CharSequence contentText = "Vào KQXS để biết thêm chi tiết!";
		if (quit) {
			notifyIntent = new Intent(context, FlashScreen.class);
		} else {
			notifyIntent = new Intent();
		}
		b.putString("json_push", json);
		notifyIntent.putExtras(b);

		PendingIntent intentP = PendingIntent.getActivity(context, 0,
				notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
		notifyDetails.setLatestEventInfo(context, contentTitle, contentText,
				intentP);
		notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(context.NOTIFICATION_SERVICE);
		mNotificationManager.notify(0, notifyDetails);
	}

	public static String replaceAll(String source, String pattern,
			String replacement) {

		// If source is null then Stop
		// and retutn empty String.
		if (source == null) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		// Intialize Index to -1
		// to check agaist it later
		int idx = -1;
		// Search source from 0 to first occurrence of pattern
		// Set Idx equal to index at which pattern is found.

		String workingSource = source;

		// Iterate for the Pattern till idx is not be -1.
		while ((idx = workingSource.indexOf(pattern)) != -1) {
			// append all the string in source till the pattern starts.
			sb.append(workingSource.substring(0, idx));
			// append replacement of the pattern.
			sb.append(replacement);
			// Append remaining string to the String Buffer.
			sb.append(workingSource.substring(idx + pattern.length()));

			// Store the updated String and check again.
			workingSource = sb.toString();

			// Reset the StringBuffer.
			sb.delete(0, sb.length());
		}

		return workingSource;
	}

	public static SpannableStringBuilder makeLink(String text,
			ClickableSpan action, int color, int style) {
		// text = " " + text + " ";
		SpannableStringBuilder strBuilder = new SpannableStringBuilder(text);
		strBuilder.setSpan(action, 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		StyleSpan bss = new StyleSpan(style);
		strBuilder.setSpan(bss, 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		ForegroundColorSpan fcs = new ForegroundColorSpan(color);
		strBuilder.setSpan(fcs, 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		return strBuilder;
	}

	// CAU
//	public static CauPosition getCauPosition(int value) {
//		if (Global.mien == 2) {
//			return getCauPositionMT(value);
//		} else if (Global.mien == 3)
//			return getCauPositionMN6so(value);
//
//		CauPosition cauPosition = new CauPosition();
//		cauPosition.isRed = true;
//		if (value >= 0 && value < 5) {
//			cauPosition.giaiIndex = 0;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value;
//		} else if (value >= 5 && value < 10) {
//			cauPosition.giaiIndex = 1;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 5;
//		} else if (value >= 10 && value < 15) {
//			cauPosition.giaiIndex = 2;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 10;
//		} else if (value >= 15 && value < 20) {
//			cauPosition.giaiIndex = 2;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 15;
//		} else if (value >= 20 && value < 25) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 20;
//		} else if (value >= 25 && value < 30) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 25;
//		} else if (value >= 30 && value < 35) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 2;
//			cauPosition.posIndex = value - 30;
//		} else if (value >= 35 && value < 40) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 3;
//			cauPosition.posIndex = value - 35;
//		} else if (value >= 40 && value < 45) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 4;
//			cauPosition.posIndex = value - 40;
//		} else if (value >= 45 && value < 50) {
//			cauPosition.giaiIndex = 3;
//			cauPosition.resultIndex = 5;
//			cauPosition.posIndex = value - 45;
//		} else if (value >= 50 && value < 54) {
//			cauPosition.giaiIndex = 4;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 50;
//		} else if (value >= 54 && value < 58) {
//			cauPosition.giaiIndex = 4;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 54;
//		} else if (value >= 58 && value < 62) {
//			cauPosition.giaiIndex = 4;
//			cauPosition.resultIndex = 2;
//			cauPosition.posIndex = value - 58;
//		} else if (value >= 62 && value < 66) {
//			cauPosition.giaiIndex = 4;
//			cauPosition.resultIndex = 3;
//			cauPosition.posIndex = value - 62;
//		} else if (value >= 66 && value < 70) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 66;
//		} else if (value >= 70 && value < 74) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 70;
//		} else if (value >= 74 && value < 78) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 2;
//			cauPosition.posIndex = value - 74;
//		} else if (value >= 78 && value < 82) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 3;
//			cauPosition.posIndex = value - 78;
//		} else if (value >= 82 && value < 86) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 81;
//			cauPosition.posIndex = value - 82;
//		} else if (value >= 86 && value < 90) {
//			cauPosition.giaiIndex = 5;
//			cauPosition.resultIndex = 5;
//			cauPosition.posIndex = value - 86;
//		} else if (value >= 90 && value < 93) {
//			cauPosition.giaiIndex = 6;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 90;
//		} else if (value >= 93 && value < 96) {
//			cauPosition.giaiIndex = 6;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 93;
//		} else if (value >= 96 && value < 99) {
//			cauPosition.giaiIndex = 6;
//			cauPosition.resultIndex = 2;
//			cauPosition.posIndex = value - 96;
//		} else if (value >= 99 && value < 101) {
//			cauPosition.giaiIndex = 7;
//			cauPosition.resultIndex = 0;
//			cauPosition.posIndex = value - 99;
//		} else if (value >= 101 && value < 103) {
//			cauPosition.giaiIndex = 7;
//			cauPosition.resultIndex = 1;
//			cauPosition.posIndex = value - 101;
//		} else if (value >= 103 && value < 105) {
//			cauPosition.giaiIndex = 7;
//			cauPosition.resultIndex = 2;
//			cauPosition.posIndex = value - 103;
//		} else if (value >= 105) {
//			cauPosition.giaiIndex = 7;
//			cauPosition.resultIndex = 3;
//			cauPosition.posIndex = value - 105;
//		}
//	}

	// CAU
	public static CauPosition getCauPosition(int value) {
		if (Global.mien == 2) {
			return getCauPositionMT(value);
		} else if (Global.mien == 3)
			return getCauPositionMN6so(value);

		CauPosition cauPosition = new CauPosition();
		cauPosition.isRed = true;
		if (value >= 0 && value < 5) {
			cauPosition.posOfAll = 1;
			cauPosition.giaiIndex = 0;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value;
		} else if (value >= 5 && value < 10) {
			cauPosition.posOfAll = 2;
			cauPosition.giaiIndex = 1;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 5;
		} else if (value >= 10 && value < 15) {
			cauPosition.posOfAll = 3;
			cauPosition.giaiIndex = 2;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 10;
		} else if (value >= 15 && value < 20) {
			cauPosition.posOfAll = 4;
			cauPosition.giaiIndex = 2;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 15;
		} else if (value >= 20 && value < 25) {
			cauPosition.posOfAll = 5;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 20;
		} else if (value >= 25 && value < 30) {
			cauPosition.posOfAll = 6;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 25;
		} else if (value >= 30 && value < 35) {
			cauPosition.posOfAll = 7;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 30;
		} else if (value >= 35 && value < 40) {
			cauPosition.posOfAll = 8;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 35;
		} else if (value >= 40 && value < 45) {
			cauPosition.posOfAll = 9;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 4;
			cauPosition.posIndex = value - 40;
		} else if (value >= 45 && value < 50) {
			cauPosition.giaiIndex = 3;
			cauPosition.posOfAll = 10;
			cauPosition.resultIndex = 5;
			cauPosition.posIndex = value - 45;
		} else if (value >= 50 && value < 54) {
			cauPosition.posOfAll = 11;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 50;
		} else if (value >= 54 && value < 58) {
			cauPosition.posOfAll = 12;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 54;
		} else if (value >= 58 && value < 62) {
			cauPosition.posOfAll = 13;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 58;
		} else if (value >= 62 && value < 66) {
			cauPosition.posOfAll = 14;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 62;
		} else if (value >= 66 && value < 70) {
			cauPosition.posOfAll = 15;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 66;
		} else if (value >= 70 && value < 74) {
			cauPosition.posOfAll = 16;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 70;
		} else if (value >= 74 && value < 78) {
			cauPosition.posOfAll = 17;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 74;
		} else if (value >= 78 && value < 82) {
			cauPosition.posOfAll = 18;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 78;
		} else if (value >= 82 && value < 86) {
			cauPosition.posOfAll = 19;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 81;
			cauPosition.posIndex = value - 82;
		} else if (value >= 86 && value < 90) {
			cauPosition.posOfAll = 20;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 5;
			cauPosition.posIndex = value - 86;
		} else if (value >= 90 && value < 93) {
			cauPosition.posOfAll = 21;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 90;
		} else if (value >= 93 && value < 96) {
			cauPosition.posOfAll = 22;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 93;
		} else if (value >= 96 && value < 99) {
			cauPosition.posOfAll = 23;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 96;
		} else if (value >= 99 && value < 101) {
			cauPosition.posOfAll = 24;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 99;
		} else if (value >= 101 && value < 103) {
			cauPosition.posOfAll = 25;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 101;
		} else if (value >= 103 && value < 105) {
			cauPosition.posOfAll = 26;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 103;
		} else if (value >= 105) {
			cauPosition.posOfAll = 27;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 105;
		}
		return cauPosition;
	}
	public static CauPosition getCauPositionMT(int value) {
		CauPosition cauPosition = new CauPosition();
		cauPosition.isRed = true;
		if (value >= 0 && value < 5) {
			cauPosition.posOfAll = 1;
			cauPosition.giaiIndex = 0;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value;
		} else if (value >= 5 && value < 10) {
			cauPosition.posOfAll = 2;
			cauPosition.giaiIndex = 1;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 5;
		} else if (value >= 10 && value < 15) {
			cauPosition.posOfAll = 3;
			cauPosition.giaiIndex = 2;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 10;
		} else if (value >= 15 && value < 20) {
			cauPosition.posOfAll = 4;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 15;
		} else if (value >= 20 && value < 25) {
			cauPosition.posOfAll = 5;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 20;
		} else if (value >= 25 && value < 30) {
			cauPosition.posOfAll = 6;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 25;
		} else if (value >= 30 && value < 35) {
			cauPosition.posOfAll = 7;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 30;
		} else if (value >= 35 && value < 40) {
			cauPosition.posOfAll = 8;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 35;
		} else if (value >= 40 && value < 45) {
			cauPosition.posOfAll = 9;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 40;
		} else if (value >= 45 && value < 50) {
			cauPosition.posOfAll = 10;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 4;
			cauPosition.posIndex = value - 45;
		} else if (value >= 50 && value < 55) {
			cauPosition.posOfAll = 11;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 5;
			cauPosition.posIndex = value - 50;
		} else if (value >= 55 && value < 60) {
			cauPosition.posOfAll = 12;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 6;
			cauPosition.posIndex = value - 55;
		} else if (value >= 60 && value < 64) {
			cauPosition.posOfAll = 13;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 60;
		} else if (value >= 64 && value < 68) {
			cauPosition.posOfAll = 14;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 64;
		} else if (value >= 68 && value < 72) {
			cauPosition.posOfAll = 15;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 68;
		} else if (value >= 72 && value < 76) {
			cauPosition.posOfAll = 16;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 72;
		} else if (value >= 76 && value < 79) {
			cauPosition.posOfAll = 17;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 76;
		} else if (value >= 79 && value < 81) {
			cauPosition.posOfAll = 18;
			cauPosition.giaiIndex = 8;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 79;
		}

		return cauPosition;
	}

	public static CauPosition getCauPositionMN6so(int value) {
		CauPosition cauPosition = new CauPosition();
		cauPosition.isRed = true;
		if (value >= 0 && value < 6) {
			cauPosition.posOfAll = 1;
			cauPosition.giaiIndex = 0;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value;
		} else if (value >= 6 && value < 11) {
			cauPosition.posOfAll = 2;
			cauPosition.giaiIndex = 1;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 6;
		} else if (value >= 11 && value < 16) {
			cauPosition.posOfAll = 3;
			cauPosition.giaiIndex = 2;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 11;
		} else if (value >= 16 && value < 21) {
			cauPosition.posOfAll = 4;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 16;
		} else if (value >= 21 && value < 26) {
			cauPosition.posOfAll = 5;
			cauPosition.giaiIndex = 3;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 21;
		} else if (value >= 26 && value < 31) {
			cauPosition.posOfAll = 6;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 26;
		} else if (value >= 31 && value < 36) {
			cauPosition.posOfAll = 7;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 31;
		} else if (value >= 36 && value < 41) {
			cauPosition.posOfAll = 8;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 36;
		} else if (value >= 41 && value < 46) {
			cauPosition.posOfAll = 9;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 3;
			cauPosition.posIndex = value - 41;
		} else if (value >= 46 && value < 51) {
			cauPosition.posOfAll = 10;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 4;
			cauPosition.posIndex = value - 46;
		} else if (value >= 51 && value < 56) {
			cauPosition.posOfAll = 11;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 5;
			cauPosition.posIndex = value - 51;
		} else if (value >= 56 && value < 61) {
			cauPosition.posOfAll = 12;
			cauPosition.giaiIndex = 4;
			cauPosition.resultIndex = 6;
			cauPosition.posIndex = value - 56;
		} else if (value >= 61 && value < 65) {
			cauPosition.posOfAll = 13;
			cauPosition.giaiIndex = 5;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 61;
		} else if (value >= 65 && value < 69) {
			cauPosition.posOfAll = 14;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 65;
		} else if (value >= 69 && value < 73) {
			cauPosition.posOfAll = 15;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 1;
			cauPosition.posIndex = value - 69;
		} else if (value >= 73 && value < 77) {
			cauPosition.posOfAll = 16;
			cauPosition.giaiIndex = 6;
			cauPosition.resultIndex = 2;
			cauPosition.posIndex = value - 73;
		} else if (value >= 77 && value < 80) {
			cauPosition.posOfAll = 17;
			cauPosition.giaiIndex = 7;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 77;
		} else if (value >= 80 && value < 82) {
			cauPosition.posOfAll = 18;
			cauPosition.giaiIndex = 8;
			cauPosition.resultIndex = 0;
			cauPosition.posIndex = value - 80;
		}

		return cauPosition;
	}

	public static void showFailureMessage(Context context, String msg) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle("Thông báo!");
		alert.setMessage(msg);
		alert.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// goToMainProgram();
					}
				});
		alert.show();
	}

	public static SpannableStringBuilder makeText(String text, int color,
			int style, int fontSize) {
		SpannableStringBuilder strBuilder = new SpannableStringBuilder(text);

		StyleSpan bss = new StyleSpan(style);
		strBuilder.setSpan(bss, 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		ForegroundColorSpan fcs = new ForegroundColorSpan(color);
		strBuilder.setSpan(fcs, 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		strBuilder.setSpan(new AbsoluteSizeSpan(fontSize), 0, text.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return strBuilder;
	}

	public static Executor creatForceAsyncTaskThreadPool() {
		int corePoolSize = 60;
		int maximumPoolSize = 80;
		int keepAliveTime = 10;

		BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(
				maximumPoolSize);
		Executor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
				maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
		return threadPoolExecutor;
	}

	public static boolean isUnableSms(Context context) {
		return !context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_TELEPHONY);
	}

}
